# -*- coding: utf-8 -*-

import os
import json
import time
from pprint import pprint

from year_data import YearData, get_every_year_avg


def calculate(data_dir):
    print(data_dir)
    x = get_every_year_avg(data_dir)
    print(x["year_avg"])
    print(x["growth_avg"])


def make_json():
    root = r"E:\毕设数据"
    dirs = os.listdir(root)
    keys_I = []
    dict_I = {}
    for i in dirs:
        if i.endswith("Excel"):
            keys_I.append(i)
    for j in keys_I:
        dir_I = os.path.join(root, j)
        dict_I[j] = os.listdir(dir_I)
        # for k in os.listdir(dir_I):

        # break
    # pprint(dict_I)
    for i in dict_I:
        if i == "WuhanNDVI_Excel":
            path = os.path.join(root, i)
            print(path)
            avg = get_every_year_avg(path)
            dict_I[i] = {"year_avg": avg['year_avg'], "growth_avg": avg['growth_avg']}

            # print(avg)
        else:
            # print(dict_I[i])
            tmp = {}
            for j in dict_I[i]:
                # print(j)
                path = os.path.join(os.path.join(root, i), j)
                print(path)
                avg = get_every_year_avg(path)
                tmp[j] = {"year_avg": avg['year_avg'], "growth_avg": avg['growth_avg']}
                # dict_I[i] = j
            dict_I[i] = tmp
    # pprint(dict_I)
    with open("avgRecord.json", 'w') as f:
        json.dump(dict_I, f, ensure_ascii=False)


if __name__ == "__main__":
    # d = r"E:\毕设数据\VegetationTypeNDVI_Excel\阔叶"
    # calculate(d)
    x = time.time()
    make_json()
    y = time.time()
    cost_time = (y - x) / 60
    print("cost time %.2f min" % cost_time)
